
jQuery(function($){
    //回车事件
    $(document).keydown(function(event) {
        var key = window.event ? event.keyCode : event.which;
        if(key==13){
            if($("#hotelID").is(":focus")){
                $("#hotelID").valid();
				$("#bmh").focus();
                $("#bmh").select();
                return false;
            }
            
            if($("#bmh").is(":focus")){
				$("#bmh").valid();
				$("#userid").focus();
				$("#userid").select();
				return false;
            }
            
            if($("#userid").is(":focus")){
				$("#upass").focus();
				return false;
            }
            
            if($("#upass").is(":focus")){	
				$("#kn").focus();
				return false;
            }
            
            if($("#kn").is(":focus")){	
				if($("#id-disable-check").is(':checked')){
					localStorage.setItem("posHotelId",$("#hotelID").val());
					localStorage.setItem("posBmh",$("#bmh").val());
                }
                $("#loginButton").trigger("click"); 
			}
        }
    });

    // 验证登录表单 
	$("#loginButton").bind("click",function(){
        $('#validationLoginForm').submit(); 
    });

    //登录验证
    $('#validationLoginForm').validate({
        errorElement : 'div',
		errorClass : 'help-block',
		focusInvalid : false,
        ignore : "",
        rules : {
            userid:{
				required : true,
			},
			hotelID:{
				required : true,

			},
			bmh:{
				required : true,
			},
			kn:{
				required : true,
				maxlength:1,
				number: true,
			}
        },
        messages : {
            userid : {
				required : "请填写用户名",
			},
			hotelID:{
				required : "请填写酒店编号",
			},
			bmh:{
				required : "请填写部门号", 
			},
			kn:{
				required : "请填写班次", 
				maxlength:"请输入合法的班次如0",
				number:"请输入合法的班次如0",
			}
        },
        highlight : function(e) {
			$(e).closest('label').removeClass('has-info').addClass('has-error');
        },
        success : function(e) {
			$(e).closest('label').removeClass('has-error');
			$(e).remove();
        },
        errorPlacement : function(error, element) {
			error.insertAfter(element.parent());
        },
        submitHandler : function(form) {
            if($("#kn").val()==""){
				$("#loginTip").html("请选择班次");
				return
			}else{
				$("#loginTip").html("");
            }
            
            var hotelId = $("#hotelID").val();
            var bmh = $("#bmh").val();
            var userid = $("#userid").val();
            var upass = $("#upass").val();
            var jn = {"arg":{"userid":userid,"upass":upass,"bmh":bmh,kn:$("#kn").val()}};
            var jnJson2 = {};
            var fu = "HoLogin";
            $.HSAPI_HOLOGIN(hotelId,bmh,userid,upass,jn,jnJson2,fu,function(data){
                if(data.RETURN&&data.RETURN<0){
                    $.HSAPI_ALERT(data.MSG);
                    $("#loginTip").html(data.MSG);
                }else{
                    localStorage.setItem("hotelId",hotelId);
                    localStorage.setItem("bmh",bmh);
                    localStorage.setItem("userid",userid);
                    localStorage.setItem("token",data.token);
                    window.location.href = "../../spaindex.html";
                }
            },function(data){
                $.HSAPI_ALERT(JSON.stringify(data))
            })
        },
    });

    if(!!localStorage.getItem("posHotelId")){
        $("#hotelID").val(localStorage.getItem("posHotelId")||"");
        $("#bmh").val(localStorage.getItem("posBmh")||"");
        $("#userid").val(localStorage.getItem("posUserid")||"");
        $("#upass").val(localStorage.getItem("posUpass")||"");
        $("#kn").val(localStorage.getItem("posKn")||""); 
        $("#id-disable-check").prop("checked",true);
    
    }else{
        $("#hotelID").focus();
    }
    if(!!localStorage.getItem("posBmh")){
        $("#bmh").val(localStorage.getItem("posBmh"));
    }
    
    //显示字母键盘 
    $(".keyboard_abc").click(function(){
        $("#keyboard").toggle();
        $("#keyboard").draggable();
    });
    
    //返回
    $(".keyboard_bk").click(function(){
        $("#keyboard").hide();
    });
    
    //退格
    $(".keyboard_bks").click(function(){
        var msg = $("input.focus").val();
        msg = msg.substring(0,msg.length-1);
        $("input.focus").val(msg);
    });
    
    //input选中
    $("#validationLoginForm input").click(function(){
        $("#validationLoginForm input").removeClass("focus");
        $(this).addClass("focus");
    }); 
    
    //数字输入
    $("#num_keyboard button").click(function(){
        if(!$(this).parents("li").hasClass("keyboard_bks") && !$(this).parents("li").hasClass("keyboard_abc")){
            var msg = $("input.focus").val();
            msg = msg + $(this).text();
            $("input.focus").val(msg);
            $("input.focus").focus();
        }
    });
    
    //数字输入
    $("#keyboard button").click(function(){
        if(!$(this).parents("li").hasClass("keyboard_bks") && !$(this).parents("li").hasClass("keyboard_bk")){
            var msg = $("input.focus").val();
            msg = msg + $(this).text();
            $("input.focus").val(msg);
        }
    });
});